package com.sczmgk.hotline_pzh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.nineoldandroids.view.ViewHelper;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.mainhomeUiView.MainHomeApplication;
import com.sczmgk.hotline_pzh.mainhomeUiView.MainHomeComplaintSetting;
import com.sczmgk.hotline_pzh.mainhomeUiView.MainHomeMe;
import com.sczmgk.hotline_pzh.sp.CacheUtils;
import com.sczmgk.hotline_pzh.ui.LoginActivity;
import com.sczmgk.hotline_pzh.utils.CheckboxUtils;
import com.sczmgk.hotline_pzh.utils.MyLog;
import com.sczmgk.hotline_pzh.view.MyHeadView;
import com.sczmgk.hotline_pzh.view.MyViewPager;

import java.util.ArrayList;
import java.util.List;

import static com.sczmgk.hotline_pzh.dialog.DialogFactory.chooseDialog;
import static com.sczmgk.hotline_pzh.ui.MainActivity.REQUEST_CODE_LOGIN;

/**
 * Created by Administrator on 2016/2/25.
 */
public class MainHomeFragment extends Fragment {

    private final String TAG = MainHomeFragment.class.getSimpleName();

    /**
     * 上一次展示的页面
     */
    private int lastPosition;

    private MyViewPager mViewPager;
    /**
     * 菜单的文字
     */
    private String[] menuTitles;
    /**
     * 菜单图片的id
     */
    private int[] menuIcons;
    private View rootView;
    private MyHeadView mHeadView;
    private RadioGroup mRadioGroup;
    private List<View> lists;
    private ImageView mHomeBg;
    public RadioButton mComplaintSettingRb;
    private RadioButton mHomeRb;
    private RadioButton mMe;

    public void setBgAlpha(float percent) {
        ViewHelper.setAlpha(mHomeBg, percent);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.main_home_base, null);

        mComplaintSettingRb = (RadioButton) rootView.findViewById(R.id.rb_my_complaint_setting);
        mHomeRb = (RadioButton) rootView.findViewById(R.id.rb_main_home);
        mMe = (RadioButton) rootView.findViewById(R.id.rb_me);

        return rootView;
    }

    private MainHomeApplication mainHomeApplication;
    private MainHomeComplaintSetting mainHomeComplaintSetting;
    private MainHomeMe mainHomeMe;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //实始化顶部title
        mHomeBg = (ImageView) rootView.findViewById(R.id.iv_main_home_bg);
        mHeadView = (MyHeadView) rootView.findViewById(R.id.headview);
        mHeadView.setLeftBtnVisibility(View.GONE);
        mHeadView.setTitle("");
        mHeadView.setVisibility(View.GONE);

        mViewPager = (MyViewPager) rootView.findViewById(R.id.vp_main_home);

        //初始化四个主页的View
        lists = new ArrayList<>();

        //应用
        mainHomeApplication = new MainHomeApplication(getActivity(), mComplaintSettingRb, mMe);
        View mainHomeApplicationView = mainHomeApplication.getView();
        lists.add(mainHomeApplicationView);

        mainHomeComplaintSetting = new MainHomeComplaintSetting(getActivity());
        View complaintSetting = mainHomeComplaintSetting.getView();
        lists.add(complaintSetting);

        mainHomeMe = new MainHomeMe(getActivity(), mHomeRb, mComplaintSettingRb);
        View me = mainHomeMe.getView();
        lists.add(me);

        mViewPager.setAdapter(new MyViewPagerAdapter());

        //初始化底部导航
        initRadioButton();

//        setMainHomeOnResume();//设置主页取得焦点
    }

    /**
     * 初始化RadioButton
     */
    private void initRadioButton() {

        //图片及文字的资源
        menuTitles = getActivity().getResources().getStringArray(R.array.main_home_menu);
        menuIcons = new int[]{R.drawable.menu_main_home, R.drawable.menu_complaint_setting, R.drawable.menu_me};
        mRadioGroup = (RadioGroup) rootView.findViewById(R.id.rg_nav);//找到RadioGroup

        for (int i = 0; i < mRadioGroup.getChildCount(); i++) {//迭代设置各个RadioButton图片及资源
            RadioButton rb = (RadioButton) mRadioGroup.getChildAt(i);//找到RadioButton

            CheckboxUtils.setRadioButtonImage(rb, getActivity(), menuIcons[i]);

            rb.setText(menuTitles[i]);//设置文字

            if (i == 0) {//将第一个设置成选中状态
                rb.setChecked(true);
                mViewPager.setCurrentItem(0);
                mHeadView.setTitle(R.string.main_home);
//                setMainHomeOnResume();
            }
        }

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_main_home:
                        lastPosition = checkedId;
                        MyLog.d(TAG, "home checked-------》 ");
                        mViewPager.setCurrentItem(0);
                        setMainHomeOnResume();
                        break;
                    case R.id.rb_my_complaint_setting:
                        MyLog.d(TAG, "complain checked-------》 ");

                        //当没有登录时弹出Dialog提示登录
                        if (!CacheUtils.getBooleanData(getActivity(), Constants.IS_LOGIN_KEY, false)) {
                            mRadioGroup.check(lastPosition);
                            chooseDialog(getActivity(), getActivity().getResources().getString(R.string.dialog_login_show),
                                    R.string.sure, R.string.cancel, new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                                            getActivity().startActivityForResult(intent, REQUEST_CODE_LOGIN);
                                        }
                                    }, new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                        }
                                    }).show();

                        } else {
                            lastPosition = checkedId;
                            mViewPager.setCurrentItem(1);
                            mainHomeComplaintSetting.mHandler.sendEmptyMessage(Constants.CHECK_COMPLAINT_VIEW);
                            setComplaintSettingOnResume();
                        }

                        break;
                    case R.id.rb_me:
                        lastPosition = checkedId;
                        MyLog.d(TAG, "me checked-------》 ");
                        mViewPager.setCurrentItem(2);
                        setMeOnResume();
                        break;
                }
            }
        });

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                MyLog.d(TAG, "onPageSelected           position: " + position);
                RadioButton rb = (RadioButton) mRadioGroup.getChildAt(position);
                rb.setChecked(true);

            }
        });
    }

    /**
     * 设置主页得到焦点其它失去焦点
     */
    private void setMainHomeOnResume() {
        mainHomeApplication.onResume();
        mainHomeComplaintSetting.onPause();
        mainHomeMe.onPause();
    }

    /**
     * 设置投诉处理得到焦点
     */
    private void setComplaintSettingOnResume() {
        mainHomeComplaintSetting.onResume();
        mainHomeApplication.onPause();
        mainHomeMe.onPause();
    }

    /**
     * 设置我的得到焦点
     */
    private void setMeOnResume() {
        mainHomeMe.onResume();
        mainHomeApplication.onPause();
        mainHomeComplaintSetting.onPause();

    }

    private class MyViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            container.addView(lists.get(position));
            return lists.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(lists.get(position));
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        MyLog.d(TAG, "MainFragment  onPause ");
        mainHomeApplication.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        MyLog.i(TAG, "MainFragment  onresume");
        int checkedId = mRadioGroup.getCheckedRadioButtonId();
        switch (checkedId) {
            case R.id.rb_main_home:
                setMainHomeOnResume();
                break;
            case R.id.rb_my_complaint_setting:
                if (flag == Constants.ONLINE_COMPLAINT_FLAG) {
                    flag = 0;
                } else {
                    setComplaintSettingOnResume();
                }
                break;
            case R.id.rb_me:
                setMeOnResume();
                break;
        }

    }

    //    public void clearComplaintSettingData(){
//        mainHomeComplaintSetting.clearText();
//    }

    public void goToComplaintSetting() {
        mComplaintSettingRb.setChecked(true);
    }

    public void goToMainHome() {
        mHomeRb.setChecked(true);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onDestroy() {
        mainHomeComplaintSetting.onDestroy();
        mainHomeMe.onDestroy();
        mainHomeApplication.onDestroy();
        super.onDestroy();

    }


    public void toMainApplication() {
        setMainHomeOnResume();
    }

    /*传递的标签*/
    protected int flag;

    public void setComplaintFlag(int flag) {
        this.flag = flag;
    }
}
